<template>
  <view>
    <view class="hrader">
      <image
        src="../../../static/bback.png"
        class="goback"
        @click="back"
        mode=""
      />
      我的发布
    </view>
    <view class="content" style="padding-top: 108rpx">
      <view class="lis" v-for="(gambit, index) in Topiclist" :key="index">
        <view class="top">
          <image class="avtar" :src="userInfo.avatar" mode="aspectFit" />
          <view class="rig">
            <view class="name">{{ userInfo.nickname }}</view>
            <view class="tiem">{{ gambit.createtime | formatDate }}</view>
          </view>
          <view class="guanzhu" @click="deleteArticle(gambit.id)">
            <image
              class="img"
              src="../../../static/PersonalCenter/fbsc.png"
              mode=""
            />
            删除
          </view>
        </view>
        <view class="tetx">
          <view
            class="tag"
            @click="join(gambit, gambit.topic_id, gambit.topicTitle)"
          >
            <span>#</span>{{ gambit.topicTitle }}
          </view>
          {{ gambit.content }}
        </view>
        <view class="imglis">
          <image
            v-for="(item, index) in gambit.images"
            v-show="item.length == 0 ? false : true"
            :key="index"
            class="imgls"
            :src="$IMG_URL + item"
            mode="aspectFill"
            @click="picClcik(gambit.images, index)"
          />
        </view>
      </view>
    </view>
    <view class="friends" v-show="Topiclist.length == 0">
      <image
        class="img"
        src="../../../static/PersonalCenter/nodata.png"
        mode=""
      />
      <text>您暂时没有相关记录~</text>
    </view>
    <!-- 删除弹窗 -->
    <uni-popup ref="popup" type="center">
      <view class="popCenter">
        <view class="tips">温馨提示</view>
        <text style="padding: 40rpx 0">您确定要删除这篇文章吗？</text>
        <view class="popBtn">
          <text class="cancel" @click="showClcik">取消</text>
          <text
            class="cancel"
            @click="quitClcik"
            style="border: 0; color: #c52b2e"
            >确定</text
          >
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      token: "",
      Topiclist: [],
      userInfo: "",
      imgUrl: "https://www.123qifu.com/",
      id: "",
    };
  },
  onLoad() {
    this.token = uni.getStorageSync("token");
  },
  onShow() {
    this.getmyArticle();
    this.getuserInfo();
  },
  methods: {
    back() {
      uni.switchTab({
        url: "../users",
      });
    },

    // 获取我的文章
    async getmyArticle() {
      let that = this;
      const res = await that.$myRequest({
        url: "user/myArticle",
        method: "POST",
        header: {
          token: that.token,
        },
      });
      if (res.data.code == 1) {
        that.Topiclist = res.data.data.data;
      }
    },
    // 获取用户信息
    async getuserInfo() {
      let that = this;
      const res = await that.$myRequest({
        url: "user/getUserInfo",
        method: "GET",
        header: {
          token: that.token,
        },
        data: {},
      });
      if (res.data.code == 1) {
        that.userInfo = res.data.data;
      }
    },
    // 删除文章
    async deleteArticle(id) {
      this.id = id;
      this.$refs.popup.open();
    },
    // 删除文章
    async quitClcik() {
      let that = this;
      const res = await that.$myRequest({
        url: "user/delArticle",
        method: "POST",
        data: {
          id: this.id,
        },
      });
      if (res.data.code == 1) {
        this.$refs.popup.close();
        that.getmyArticle();
      }
    },
    showClcik() {
      this.$refs.popup.close();
    },

    //图片预览
    picClcik(item, index) {
      let that = this;
      let imgsArray = [];
      for (let i = 0; i < item.length; i++) {
        imgsArray.push(that.imgUrl + item[i]);
      }
      uni.previewImage({
        urls: imgsArray,
        current: imgsArray[index],
      });
    },

    // 加入话题
    join(item, id, title) {
      uni.setStorage({
        key: "topicDetail",
        data: item,
      });
      uni.navigateTo({
        url: "../../Supplication/Supplication?id=" + id + "&title=" + title,
      });
    },
  },
};
</script>

<style lang="scss">
.hrader {
  width: 100%;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  position: relative;
  font-size: 36rpx;
  font-weight: 500;
  color: #333333;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 99;
  border-bottom: 20rpx solid #eee;
  .goback {
    position: absolute;
    width: 22rpx;
    height: 34rpx;
    left: 28rpx;
  }
  .reco {
    position: absolute;
    font-size: 30rpx;
    font-weight: 400;
    color: #333333;
    right: 30rpx;
  }
}
.content {
  width: 100%;
  height: auto;
  .title {
    width: 100%;
    height: 86rpx;
    box-sizing: border-box;
    padding: 0 30rpx;
    font-size: 30rpx;
    font-weight: 400;
    color: #999999;
    display: flex;
    align-items: center;
    border-bottom: 1rpx solid #eee;
  }
  .lis {
    width: 690rpx;
    min-height: 200rpx;
    box-sizing: border-box;
    padding: 30rpx 0;
    border-bottom: 1rpx solid #eee;
    overflow: hidden;
    margin: 0 auto;
    .top {
      display: flex;
      align-items: center;
      .avtar {
        display: block;
        width: 90rpx;
        height: 90rpx;
        border-radius: 50%;
        margin-right: 25rpx;
      }
      .rig {
        display: flex;
        flex-wrap: wrap;
        flex-direction: column;
        justify-content: center;
        .name {
          font-size: 30rpx;
          font-weight: 550;
          color: #333333;
        }
        .tiem {
          font-size: 20rpx;
          font-weight: 400;
          color: #999999;
        }
      }
      .guanzhu {
        position: absolute;
        right: 30rpx;
        width: 120rpx;
        height: 42rpx;
        box-sizing: border-box;
        border: 1rpx solid #e8e8e8;
        border-radius: 21rpx;
        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 26rpx;
        font-weight: 400;
        color: #999999;
        .img {
          display: block;
          width: 24rpx;
          height: 26rpx;
          margin-right: 8rpx;
        }
      }
      .yiguanzhu {
        position: absolute;
        right: 30rpx;
        width: 120rpx;
        height: 42rpx;
        border: 1rpx solid #e8e8e8;
        border-radius: 21rpx;
        text-align: center;
        line-height: 42rpx;
        font-size: 26rpx;
        font-weight: 400;
        color: #999999;
      }
    }
    .tetx {
      display: inline-block;
      margin-top: 25rpx;
      font-size: 28rpx;
      font-weight: 500;
      color: #333;
      line-height: 45rpx;
      text-align: justify;
      .tag {
        color: #303faf;
        display: inline-block;
        margin-right: 10rpx;
        white-space: nowrap;
        span {
          display: inline-block;
          margin-right: 5 rpx;
        }
      }
    }
    .imglis {
      width: 105%;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin-top: 25rpx;
      .imgls {
        display: block;
        width: 220rpx;
        height: 220rpx;
        margin-right: 15rpx;
        margin-bottom: 15rpx;
        border-radius: 8rpx;
      }
    }
  }
  .lis:last-child {
    border-bottom: none;
  }
  .friends {
    font-weight: 400;
    color: #999999;
    font-size: 30rpx;
    display: flex;
    justify-content: center;
    padding-top: 200rpx;
  }
}
.friends {
  .img {
    display: block;
    width: 348rpx;
    height: 188rpx;
    margin: 314rpx auto 0;
  }
  text {
    display: block;
    text-align: center;
    font-size: 30rpx;
    font-weight: 400;
    color: #999999;
    margin: 20rpx auto 0;
  }
}
.popCenter {
  display: flex;
  align-items: center;
  flex-direction: column;
  width: 546rpx;
  padding: 40rpx 0 0;
  background-color: #fff;
  font-size: 30rpx;
  color: #333333;
  border-radius: 10rpx;
  .tips {
    text-align: center;
    font-size: 30rpx;
    font-weight: 550;
    color: #373737;
  }
  .popBtn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100rpx;
    border-top: 1rpx solid #eee;
    border-radius: 0 0 10rpx 10rpx;

    .cancel {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 28rpx;
      color: #373737;
      width: 50%;
      height: 100%;
      border-right: 1rpx solid #eee;
      letter-spacing: 2rpx;
    }
  }
}
</style>
